
package com.zykj.evaluation.ui

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.constraintlayout.compose.ConstraintLayout
import com.zykj.evaluation.R
import com.zykj.evaluation.model.SubjectType
import com.zykj.evaluation.model.TodoTask

@Composable
fun Detail(
    modifier: Modifier = Modifier,
    count: Int = 2,
    contentPadding: PaddingValues,
    horizontalSpace: Dp,
    verticalSpace: Dp,
    todoEvaluationList: List<TodoTask>,
    onStartAnswer: (TodoTask) -> Unit
) {
    LazyVerticalGrid(
        modifier = modifier,
        columns = GridCells.Fixed(count),
        verticalArrangement = Arrangement.spacedBy(verticalSpace),
        horizontalArrangement = Arrangement.spacedBy(horizontalSpace),
        contentPadding = contentPadding
    ) {
        items(todoEvaluationList) {
            QuestionItem(it, onStartAnswer = onStartAnswer)
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun QuestionItem(todoTask: TodoTask, onStartAnswer: (TodoTask) -> Unit) {
    Box(
        modifier = Modifier
            .height(height = 84.dp)
            .fillMaxWidth()
    ) {
        val subjectRadius = 20.dp
        val contentPadding = 8.dp
        Card(
            border = BorderStroke(1.dp, Color(0xFFDDDDDD)),
            shape = RoundedCornerShape(8.dp),
            modifier = Modifier.padding(start = subjectRadius),
        ) {
            ConstraintLayout(
                modifier = Modifier
                    .fillMaxSize()
                    .background(color = Color.White)
                    .padding(start = subjectRadius + contentPadding, end = contentPadding)
                    .padding(vertical = 10.dp),
            ) {
                val (textTitle, textTotal, textTime, textStart, textPhotography) = createRefs()
                Text(
                    text = todoTask.examName,
                    modifier = Modifier.constrainAs(ref = textTitle) {
                        top.linkTo(parent.top)
                        bottom.linkTo(textTotal.top)
                    },
                    style = MaterialTheme.typography.bodyMedium.copy(
                        color = Color(0xFF555555),
                        fontWeight = FontWeight.Medium
                    ),
                )
                Text(
                    text = stringResource(
                        id = R.string.combine_text_question,
                        todoTask.qstCount,
                        todoTask.score
                    ),
                    style = MaterialTheme.typography.bodySmall.copy(
                        color = Color(0xFF888888),
                        fontWeight = FontWeight.Medium
                    ),
                    modifier = Modifier.constrainAs(ref = textTotal) {
                        top.linkTo(textTitle.bottom)
                        bottom.linkTo(textTime.top)
                    })
                Text(
                    text = "开始时间 ${todoTask.startTime ?: todoTask.creationTime}",
                    style = MaterialTheme.typography.bodySmall.copy(
                        color = Color(0xFF555555),
                        fontWeight = FontWeight.Medium
                    ),
                    modifier = Modifier.constrainAs(ref = textTime) {
                        top.linkTo(textTotal.bottom)
                        bottom.linkTo(parent.bottom)
                    }
                )

                ActionButton(
                    modifier = Modifier
                        .constrainAs(textStart) {
                            top.linkTo(parent.top)
                            bottom.linkTo(parent.bottom)
                            end.linkTo(parent.end)
                        }
                        .padding(end = 5.dp)
                        .size(
                            width = 61.dp, height = 23.dp
                        ), label = "开始答题"
                ) {
                    onStartAnswer(todoTask)
                }

                ActionButton(
                    modifier = Modifier
                        .constrainAs(textPhotography) {
                            top.linkTo(parent.top)
                            bottom.linkTo(parent.bottom)
                            end.linkTo(textStart.start)
                        }
                        .padding(end = 5.dp)
                        .size(
                            width = 61.dp, height = 23.dp
                        ), label = "拍答题卡"
                ) {
                    //todo
                }

            }
        }
        Subject(
            modifier = Modifier
                .align(Alignment.CenterStart)
                .size(subjectRadius * 2), todoTask = todoTask
        )
    }
}

@Composable
fun ActionButton(
    modifier: Modifier = Modifier,
    shape: Shape = RoundedCornerShape(16.dp),
    fontColor: Color = Color.White,
    containerColor: Color = MaterialTheme.colorScheme.primary,
    label: String,
    onClick: () -> Unit
) {
    Button(
        onClick = {
            onClick.invoke()
        },
        modifier = modifier,
        colors = ButtonDefaults.buttonColors(containerColor = containerColor),
        shape = shape,
        contentPadding = PaddingValues(0.dp)
    ) {
        Text(
            text = label,
            color = fontColor,
            fontSize = 11.sp
        )
    }
}

fun textLabel(topicId: Int): Int {
    return when (topicId) {
        SubjectType.CHINESE.state -> SubjectType.CHINESE.stateName
        SubjectType.MATH.state -> SubjectType.MATH.stateName
        SubjectType.ENGLISH.state -> SubjectType.ENGLISH.stateName
        SubjectType.PHYSICS.state -> SubjectType.PHYSICS.stateName
        SubjectType.CHEMISTRY.state -> SubjectType.CHEMISTRY.stateName
        SubjectType.BIOLOGY.state -> SubjectType.BIOLOGY.stateName
        SubjectType.POLITIC.state -> SubjectType.POLITIC.stateName
        SubjectType.HISTORY.state -> SubjectType.HISTORY.stateName
        SubjectType.GEOLOGY.state -> SubjectType.GEOLOGY.stateName
        else -> SubjectType.CHINESE.stateName
    }
}

@Composable
private fun Subject(modifier: Modifier = Modifier, todoTask: TodoTask) {
    Box(
        modifier = modifier
            .clip(CircleShape)
            .background(color = Color.White)
            .border(
                border = BorderStroke(
                    2.dp,
                    Color(0xFFCBDEFF)
                ), shape = CircleShape
            ), contentAlignment = Alignment.Center
    ) {
        Text(
            text = stringResource(id = textLabel(todoTask.topicId)),
            color = Color(0xFFCBDEFF),
            fontSize = 11.sp
        )
    }
}